<?php

namespace Home\Controller;

/**
 * Description of HotelApplyController.class控制器实例
 * 
 * @author songanwei
 * @date 2016-1-4
 */
class HotelApplyController extends ParentController {

    /**
     * @description 客栈申请列表视图页面
     * @modify songanwei 2016-01-04
     */
    public function index() {
        $param = I('post.');
        $where = $condition = array();
        if (isset($param['k']) && $param['k']) {
            $condition['k'] = $param['k'];
            $where['_string'] = "hotelname like '%" . $param['k'] . "%' OR mobile like '%" . $param['k'] . "%'";
        }
        $where['dealerlogin_id'] = 0;
        $hotelApplyModel = D('HotelApply');
        $count = $hotelApplyModel->where($where)->count();
        $Page = classPage($count);
        $order = 'status asc,createtime desc';
        $data = $hotelApplyModel->where($where)->order($order)->limit($Page->firstRow, $Page->listRows)->select();
        $page = $Page->show();

        //获取所有省份数据
        $provinceModel = D('Province');
        $provinces = $provinceModel->getAll();
        foreach ($provinces as $key => &$provincesItem) {
           $provincesItem['name'] = explode('/', $provincesItem['name'])[0];
        }
        unset($provincesItem);
        //获取地区名称
        $districtModel = D('District');
        foreach ($data as $key => &$dataItem) {
           $disName = $districtModel->getName($dataItem['district_id']);
           $dataItem['districtName'] = !empty($disName) ? explode('/', $disName)[0] : '';
        }
        unset($dataItem);

        //获取区号时区数据
        $DealerApplyLogic = A('Common/DealerApply', 'Logic');
        $universalTimeData = $DealerApplyLogic->universalTimeList();
        $this->assign('universalTimeList', $universalTimeData);
        // 获取系统货币
        $currencyModel = D('Currency');
        $currencyData = $currencyModel->getCurrencyInfo();
        $this->assign('currencyInfo', $currencyData);

        $this->assign('page', $page);
        $this->assign('condition', $condition);
        $this->assign('applyData', $data);
        $this->assign('provinces', $provinces);
        $this->display();
    }

    /**
     * @description  更新为拒绝状态
     * @modify songanwei 2016-01-04
     */
    public function setStatus() {
        $applyId = I('post.id', 0, 'intval');
        $hotelApplyModel = D('HotelApply');
        $hotelApply = $hotelApplyModel->getInfo($applyId);
        if (empty($hotelApply)) {
            returnJson(403, 122, '数据不存在');
        }

        $result = $hotelApplyModel->setStatus($applyId, 2);
        if ($result == false) {
            returnJson(403, 123, '更新失败');
        }
        //发送拒绝短信通知
        if ($hotelApply['areacode'] == '86') {
            $sms = new \Org\Util\Sms();
            $sms->sendRefuse($hotelApply['mobile'], $hotelApply['hotelname']);
        } else {
            $sms = new \Org\Util\yunPian();
            $sms->sendRefuse($hotelApply['areacode'], $hotelApply['mobile'], $hotelApply['hotelname']);
        }
        
        returnJson(200, 200, '更新成功');
    }

    /**
     * @description  申请开通服务
     * @modify songanwei 2016-01-04
     * @modify songanwei 2017-05-11 新增 $areaCode $universalTime
     */
    public function handle() {
        $applyId = I('post.id', 0, 'intval');
        $hotelName = I('post.hotel', '', 'string');
        $mobile = I('post.mobile', '', 'string');
        $cityId = I('post.city', 0, 'intval');
        $districtId = I('post.district', 0, 'intval');
        $userName = I('post.username', '', 'string');
        $date = I('post.kaitongDate', 1, 'intval');
        $areaCode = I('post.areacode', '86', 'intval');
        $universalTime = I('post.universalTime', '5', 'intval');
		$currencyId = I('post.currencyId', 0, 'intval');

        //检查参数
		$date = date('Y-m-d H:i:s', strtotime("+$date year"));
		if($date<0){
		    $date = 1;
		}

        $hotelApplyLogic = A('Common/HotelApply', 'Logic');
        $hotelApplyLogic->handle($applyId, $mobile, $hotelName, $cityId, $districtId, $userName, $date, 1, $areaCode, $universalTime, $currencyId);
        returnJson(200, 200, '开通申请成功');
    }
	/**
     * @description  申请开通服务
     * @modify songanwei 2016-01-04
     */
    public function shiyong() {
        $applyId = I('post.id', 0, 'intval');
        $hotelName = I('post.hotel', '', 'string');
        $mobile = I('post.mobile', '', 'string');
        $cityId = I('post.city', 0, 'intval');
        $districtId = I('post.district', 0, 'intval');
        $userName = I('post.username', '', 'string');
		$date = I('post.shiyongDate',7, 'intval');
        $areaCode = I('post.areacode', '86', 'intval');
        $universalTime = I('post.universalTime', '5', 'intval');
        $currencyId = I('post.currencyId', 0, 'intval');
        //检查参数
		if($date<0){
		    $date = 7;
		}
		$date = date('Y-m-d H:i:s', strtotime("+$date day"));

        $hotelApplyLogic = A('Common/HotelApply', 'Logic');
        $hotelApplyLogic->handle($applyId, $mobile, $hotelName, $cityId, $districtId, $userName, $date, 2, $areaCode, $universalTime, $currencyId);
        returnJson(200, 200, '开通申请成功');
    }

    /**
     * @description  申请开通服务
     * @modify wenhui 2016-01-04
     */
    public function test() {
        $applyId = I('post.id', 0, 'intval');
        $hotelName = I('post.hotel', '', 'string');
        $mobile = I('post.mobile', '', 'string');
        $cityId = I('post.city', 0, 'intval');
        $districtId = I('post.district', 0, 'intval');
        $userName = I('post.username', '', 'string');
        $date = I('post.testDate',1, 'intval');
        $areaCode = I('post.areacode', '86', 'intval');
        $universalTime = I('post.universalTime', '5', 'intval');
        $currencyId = I('post.currencyId', 0, 'intval');
        //检查参数
        if($date<0){
            $date = 1;
        }
        $date = date('Y-m-d H:i:s', strtotime("+$date year"));

        $hotelApplyLogic = A('Common/HotelApply', 'Logic');
        $hotelApplyLogic->handle($applyId, $mobile, $hotelName, $cityId, $districtId, $userName, $date, 3, $areaCode, $universalTime, $currencyId);
        returnJson(200, 200, '开通申请成功');
    }

     /**
     * @description  更新为拒绝状态
     * @modify songanwei 2016-01-04
     */
    public function getCountryProvince() {
        $param = I('post.');

        //获取所有省份数据
        $provinceModel = D('Province');
        $provinces = $provinceModel->getAllByCountry($param['countryId']);
        foreach ($provinces as $key => &$provincesItem) {
           $provincesItem['name'] = explode('/', $provincesItem['name'])[0];
        }
        unset($provincesItem);
        returnJson(200, 200, $provinces);
    }

}
